app.service("goodsService", function($http, ajaxRequest) {
	return {
		getListByWarehouseId : function(obj) {
			return ajaxRequest.request($http.get("/selectgoods/getGoodsData", {
				params : obj
			}));
		}
	}
}).controller("goodsSelectController", function($scope, goodsService) {
	
	var mid = GetQueryString("warehouseId");
	$scope.searchData = {
		sku : $scope.sku,
		barcode : $scope.barcode,
		goodsName : $scope.goodsName,
		warehouseId : mid,
		pageIndex : 1,
		pageSize : 6
	};

	var requestListData = function() {
		$scope.searchData.sku = $scope.sku;
		$scope.searchData.barcode = $scope.barcode;
		$scope.searchData.goodsName = $scope.goodsName;
		$scope.searchData.warehouseId = mid;
		goodsService.getListByWarehouseId($scope.searchData).then(function(data) {
			var re = data.result;
			$scope.goodsList = re.entityList;
			$scope.CurrentPage = re.curPageIndex;
			$scope.RecordCount = re.recordCount;
			$scope.ShowPages = re.pageCountList;
			$scope.PageCount = re.pageCount;
		}, function(data) {
           Kalert(data.msg);
		});
	}

	requestListData();

	$scope.Search = function() {
		$scope.searchData.pageIndex = 1
		requestListData();
	}
		
	$scope.GotoPage = function(pageindex) {
		if (pageindex <= 0)
			return;
		if (pageindex > $scope.PageCount)
			return;
		$scope.searchData.pageIndex = pageindex;
		requestListData($scope.searchData);
	};
	
	$scope.sel = function(index) {
		for (var i = 0; i < $scope.goodsList.length; i++) {
			if (i == index) {
				if($scope.goodsList[i].IsSelected == false){
					$scope.goodsList[i].IsSelected = false;
				}else{
					$scope.goodsList[i].IsSelected = true;
				}
			} 
		}
	}
	
	$scope.dbc = function(index){
		$scope.goodsList[index].IsSelected = true;
		confirmGoods();
	}
	
	$scope.confirm = function() {
		confirmGoods();
	}
	
	var confirmGoods = function(){
		var IDArry = [];
		angular.forEach($scope.goodsList, function(go) {
			var si = go.IsSelected;
			if (si == true && si != undefined) {
				IDArry.push({
					goodsId : go.goodsId,
					sku : go.sku,
					goodsName : go.goodsName,
					barcode : go.barcode,
					packing : go.packing,
					curStock : go.curStock,
					curUseStock : go.curUseStock
				});
			}
		});
		if (IDArry.length == 0) {
			Kalert("请选择一条数据!", "系统提示");
			return;
		} else if (IDArry.length > 1) {
			Kalert("只能选择一条数据!", "系统提示");
			return;
		} else {
			parent.$.colorbox.close(IDArry);
		}
	}
	
	$scope.cancel = function(){
		parent.$.colorbox.close();
	}
});

function GetQueryString(sProp) {
	var re = new RegExp("[&,?]" + sProp + "=([^//&]*)", "i");
	var a = re.exec(window.location.search);
	if (a == null) {
		return "";
	} else {
		return a[1];
	}
}